package com.tim.test.leetcode.simulation.huawei;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 * @Title:
 * @Package
 * @date 2023/6/10 16:59
 * @Description:
 */
public class Simulation4 {
    public static void main(String[] args) {
        Simulation4 s = new Simulation4();
//        s.findMaxSum();
        s.checkPath();
    }


    // 找到连续k个值的最大值
    // 滑动窗口，记录前一个窗口值、最大值
    public void findMaxSum(){
        int[] nums = new int[]{2,10,-3,-8,40,5};
        int k = 4;
        int sum = 0;
        for (int i = 0; i < k; i++) {
            sum += nums[i];
        }
        int tempSum = sum;
        for (int i = k; i < nums.length; i++) {
            tempSum += nums[i]-nums[i-k];
            sum = Math.max(sum, tempSum);
        }
        System.out.println(sum);
    }

    // 通信链路检测
    // 数值可以理解为机器地址
    // 先有查询，后有响应；查询：router->host，响应：host->router；正常时，有查询必有响应，可不一一对应
    public void checkPath(){
        String info = "1 3\n" +
                "3 2\n" +
                "2 3\n" +
                "2 3\n" +
                "3 1";
        String[] msgs = info.split("\n");
        Map<String, Boolean> map = new HashMap<>();
        for (int i = 0; i < msgs.length; i++) {
            String msg = msgs[i];
            String[] split = msg.trim().split("\\s");
            if(split.length!=2){
                System.out.println(false);
                return;
            }
            int d1 = Integer.valueOf(split[0]);
            int d2 = Integer.valueOf(split[1]);
            String routerToHost = d1+","+d2;
            String hostToRouter = d2+","+d1;
            if(map.containsKey(routerToHost)){
                continue;
            }else if(map.containsKey(hostToRouter)){
                map.put(hostToRouter, true);
            }else{
                map.put(routerToHost, false);
            }
        }
        boolean flag = map.values().stream().allMatch(e -> e);
        System.out.println(flag);
    }

}
